package com.gebogebo.mobile.parking;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.gebogebo.mobile.parking.manager.ParkingSpotManager;
import com.gebogebo.mobile.parking.model.ParkingSpot;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ParkingAddSpotActivity extends MapActivity implements OnClickListener {
    private static final Random random = new Random(System.nanoTime());
    
    private static final String lg = "addParkingActivity";
    private static final ParkingSpotManager manager = new ParkingSpotManager();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_spot);
        
        setButtonClickListeners();
    }
    
    private void setButtonClickListeners() {
        findViewById(R.id.oneSpotButton).setOnClickListener(this);
        findViewById(R.id.twoToFiveSpotsButton).setOnClickListener(this);
        findViewById(R.id.sixPlusSpotsButton).setOnClickListener(this);
        
        findViewById(R.id.thirtyMin).setOnClickListener(this);
        findViewById(R.id.twoHrs).setOnClickListener(this);
        findViewById(R.id.tenHrs).setOnClickListener(this);
        findViewById(R.id.allDay).setOnClickListener(this);
        
        findViewById(R.id.freeSpot).setOnClickListener(this);
        findViewById(R.id.paidSpot).setOnClickListener(this);
        
        findViewById(R.id.addSpotButton).setOnClickListener(this);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

    public void onClick(View v) {
        int viewId = v.getId();
        
        switch(viewId) {
        case R.id.oneSpotButton:
        case R.id.twoToFiveSpotsButton:
        case R.id.sixPlusSpotsButton:
            setNumberXorButtons(viewId);
            break;
            
        case R.id.addSpotButton:
            Log.i(lg, "adding parking spot");
            new AsyncTask<Void, Void, Boolean>() {

                @Override
                protected Boolean doInBackground(Void... params) {
                    return saveParkingSpot();
                }
                
                protected void onPostExecute(Boolean result) {
                    showAddParkingMessage(result);       
                    updateParkingSpots();
                }
            }.execute();
            
        default:
            break;
        }
    }
    
    private void updateParkingSpots() {
        List<ParkingSpot> spots = manager.searchParkingSpots(0d, 0d, 0.8d, "km");
        for(ParkingSpot spot : spots) {
            int lat = (int)(spot.getLatitude() * 1E6);
            int lng = (int)(spot.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            
            OverlayItem item = new OverlayItem(point, "", "");
            ParkingSpotOverlay overlay = new ParkingSpotOverlay(getResources().getDrawable(R.drawable.marker));
            MapView map = (MapView)findViewById(R.id.mapView);
            overlay.addOverlay(item);
            map.getOverlays().add(overlay);
        }
    }
    
    private void showAddParkingMessage(boolean result) {
        Log.i(lg, "result of adding parking spot: " + result);
        if(result) {
            Toast.makeText(getApplicationContext(), getString(R.string.parking_added_success), Toast.LENGTH_LONG).show();
            return;
        }
        Toast.makeText(getApplicationContext(), getString(R.string.parking_added_unsuccess), Toast.LENGTH_LONG).show();
    }
    
    private boolean saveParkingSpot() {
        List<ParkingSpot> spots = new ArrayList<ParkingSpot>(1);
        ParkingSpot spot = new ParkingSpot();
        spot.setAmountPerSpot(10d);
        spot.setAvailableSpots(1);
        spot.setCreationDate(new Date());
        spot.setExpirationDuration(1d);
        spot.setLatitude(47.621239 + ((double)random.nextInt(5) / 100d));
        spot.setLongitude(-122.336221+ ((double)random.nextInt(5) / 100d));
        spot.setPaid(false);
        spot.setTotalSpots(2);
        spot.setUserId(2063458644l);
        
        spots.add(spot);
        return manager.addParkingSpots(spots);
    }
    
    private void setNumberXorButtons(int setButtonId) {
    }
}